﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace FirstConnectionSqlServer
{
    public class Program
    {
        private const string _connectionstring = @"DATA SOURCE=ONOFRE-NB\SQLEXPRESS;INITIAL CATALOG=BDTESTE;INTEGRATED SECURITY=TRUE;POOLING=FALSE";
        private const string _querySelect = "SELECT ID, NOME FROM TABELATESTE WHERE ID = @ID";

        public static void Main(string[] args)
        {
            List<Pessoa> pessoas = new List<Pessoa>();

            using (SqlConnection conexaoBanco = new SqlConnection(_connectionstring))
            {
                conexaoBanco.Open();
                using (SqlCommand command = new SqlCommand(_querySelect, conexaoBanco))
                {
                    command.Parameters.Add("@ID", System.Data.SqlDbType.Int);

                    command.Parameters["@ID"].Value = 6;
                    SqlDataReader reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        int id = Convert.ToInt32(reader["ID"].ToString());
                        string nome = reader["NOME"].ToString();

                        pessoas.Add(new Pessoa { id = id, Nome = nome });
                    }
                }
            }

            foreach (var item in pessoas)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
    }

    internal class Pessoa
    {
        public int id { get; set; }

        public string Nome { get; set; }

        public override string ToString()
        {
            return string.Format("{0} - {1}", id.ToString(), Nome.ToString());
        }
    }
}